export const rule = {
  examples: {
    reg: '(.*)*欢迎新人$', //匹配消息正则，命令正则
    priority: 5000, //优先级，越小优先度越高
    describe: '【#欢迎】开发简单示例演示' //【命令】功能说明
  }
};

ws.on(
  'GUILD_MEMBERS',
  async (data: { eventType: string; msg: { guild_id: string; user: { id: string } } }) => {
    if (data.eventType === 'GUILD_MEMBER_ADD') {
      let { data: res } = await client.channelApi.channels(data.msg.guild_id);

      let channel = res.find(item => item.type === 0);
      await examples({
        msg: {
          channel_id: channel.id,
          content: `<@!${data.msg.user.id}> `
        }
      });
    } else if (data.eventType === 'GUILD_MEMBER_REMOVE') {
      let { data: res } = await client.channelApi.channels(data.msg.guild_id);
      let channel = res.find(item => item.type === 0);
      await examples2({
        msg: {
          channel_id: channel.id,
          ...data.msg
        }
      });
    }
  }
);

export async function examples(e: { msg: any; eventType?: string }) {
  if (e.eventType === 'DIRECT_MESSAGE_CREATE') {
    const res = await client.directMessageApi.postDirectMessage(e.msg.guild_id, {
      content: '请在子频道中使用',
      msg_id: e.msg.id
    });
    return true;
  }
  let msg = e.msg.content.replace('欢迎新人', '');

  let message = {
    content: msg + '欢迎新人',
    image: 'http://tva1.sinaimg.cn/bmiddle/6af89bc8gw1f8ub7pm00oj202k022t8i.jpg',
    msg_id: e.msg.id // 可选，消息id，如果指定则会回复该消息
  };
  let { data } = await client.messageApi.postMessage(e.msg.channel_id, message);
  console.info(data);

  return true; //返回true 阻挡消息不再往下
}

export async function examples2(e: { msg: any }) {
  let message = {
    content: `<@!${e.msg.user.id}> ${e.msg.user.username} 离开了我们`
  };
  let { data } = await client.messageApi.postMessage(e.msg.channel_id, message);
  console.info(data);
  return true; //返回true 阻挡消息不再往下
}
